import requests
import re

url_new = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=31785"

while True:
    url = url_new
    num = re.findall(r"[0-9]+", url)

    res = requests.get(url)
    rep = res.text
    print(rep)
    nextnum = re.findall(r"[0-9]+", rep)

    if len(nextnum) == 0:
        break

    url_new = url.replace(num[0], nextnum[0])

    print(url_new)

print("end")


# http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=66831
# peak.html
# end